package com.mingtu.guards.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.config.PictureMimeType;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.MyFileListBean;

public class CommonFileAdapter extends BaseQuickAdapter<MyFileListBean, BaseViewHolder> {

    public CommonFileAdapter() {
        super(R.layout.item_file_data);
    }


    @Override
    protected void convert(@NonNull BaseViewHolder helper, MyFileListBean listBean) {
        String name = listBean.getName();
        String createTime = listBean.getCreateTime();
        String docUrl = listBean.getUrl();
        boolean isFolder = listBean.isFolder();
        boolean isCheck = listBean.isStatus();

        ImageView ivImg = helper.getView(R.id.iv_img);
        TextView tvTime = helper.getView(R.id.tv_time);
        ImageView ivCheck = helper.getView(R.id.iv_check);

        if (isFolder) {
            tvTime.setVisibility(View.GONE);
            ivImg.setBackgroundResource(R.mipmap.img_file_type1);
        } else {
            if (!StringUtils.isEmpty(docUrl)) {
                boolean isHasVideo = PictureMimeType.isUrlHasVideo(docUrl);
                boolean isHasImage = PictureMimeType.isUrlHasImage(docUrl);

                if (isHasVideo) {
                    ivImg.setBackgroundResource(R.mipmap.img_file_type4);
                } else if (isHasImage) {
                    ivImg.setBackgroundResource(R.mipmap.img_file_type3);
                } else {
                    ivImg.setBackgroundResource(R.mipmap.img_file_type2);
                }
            }
            tvTime.setVisibility(View.VISIBLE);
            tvTime.setText(createTime);
        }
        helper.setText(R.id.tv_file_name, name);
        ivCheck.setVisibility(isCheck ? View.VISIBLE : View.GONE);

    }


    /**
     * 修改状态
     *
     * @param position
     */
    public void updateStatus(int position) {
        if (mData != null && mData.size() > 0) {
            MyFileListBean myFileListBean = mData.get(position);
            myFileListBean.setStatus(true);
            mData.set(position, myFileListBean);
            notifyItemChanged(position);
        }

    }

    /**
     * 重置状态
     */
    public void resetStatus(int position) {
        if (mData != null && mData.size() > 0) {
            MyFileListBean myFileListBean = mData.get(position);
            myFileListBean.setStatus(false);
            mData.set(position, myFileListBean);
            notifyItemChanged(position);
        }
    }

}
